<template>
<div>
    <van-nav-bar title="地址信息" left-text="返回" left-arrow @click-left="onClickLeft" />
    <van-address-edit :area-list="areaList" :address-info="AddressInfo" @change-default="changeDefaultAddress" show-set-default show-search-result :show-delete="type=='edit' ? true : false" :search-result="searchResult" @save="onSave" @delete="onDelete" @change-detail="onChangeDetail" />
</div>
</template>

<script>
import {
    AddressList
} from 'vant';
import areaList from '../api/area.js';
import {
    Notify
} from 'vant';
import {
    Toast
} from "vant";
export default {
    name: "Address",
    data() {
        return {
            areaList,
            type: '',
            searchResult: [],
            AddressInfo: {},
            address_id: ''
        };
    },
    created() {
        this.type = this.$route.query.type;
        if (this.type == 'edit') {
            let AddressInfo = {};
            let addres = this.global.state.editAddressData.address.split(",");
            AddressInfo = {
                'name': this.global.state.editAddressData.name,
                'tel': this.global.state.editAddressData.tel,
                'isDefault': this.global.state.editAddressData.default ? true : false,
            };
            AddressInfo.province = addres[0];
            AddressInfo.city = addres[1];
            AddressInfo.county = addres[2];
            AddressInfo.addressDetail = addres[3];
            AddressInfo.areaCode = this.global.state.editAddressData.area;
            this.AddressInfo = AddressInfo;
        }
        const defaultAddress = this.axios.get('/api/store/user/address/list/?token=' + sessionStorage.getItem("token"));
        defaultAddress.then(res => {
            res.data.data.data.forEach(item => {
                if (item.default) {
                    console.log(item)
                    this.global.state.defaultAddress = item
                }
            })
        })

        this.address_id = this.global.state.editAddressData.R_id;
    },
    methods: {
        onClickLeft() {
            this.$router.go(-1);
        },
        onSave(e) {
            let params = {};
            params.realname = e.name;
            params.mobile = e.tel;
            params.province = e.province
            params.city = e.city;
            params.county = e.county;
            params.area = e.areaCode;
            params.address = e.addressDetail;
            params.postcode = e.postalCode;
            params.default = e.isDefault;
            params.area_code = e.area_code;
            if (this.type == 'add') {
                this.axios.post('/api/store/user/address/create/?token=' + sessionStorage.getItem("token"), params)
                    .then(res => {
                        if (res.data.code == 200) {
                            Notify({
                                type: 'success',
                                background: '#07c160',
                                message: res.data.message,
                                onOpened: () => {
                                    this.getDefaultAddress();
                                    this.$router.go(-1)
                                }
                            });
                        }
                    }).catch(error => {
                        console.log(error)
                    })
                this.global.state.addressData = e;
            } else {
                this.axios.post('/api/store/user/address/edit/' + this.address_id + '?token=' + sessionStorage.getItem("token"), params)
                    .then(res => {
                        if (res.data.code == 200) {
                            Notify({
                                type: 'success',
                                background: '#07c160',
                                message: res.data.message,
                                onOpened: () => {
                                    this.$router.go(-1)
                                }
                            });
                        }
                    }).catch(error => {
                        console.log(error)
                    })
            }

            const defaultAddress = this.axios.get('/api/store/user/address/list/?token=' + sessionStorage.getItem("token"));
            defaultAddress.then(res => {
                res.data.data.data.forEach(item => {
                    if (item.default) {
                        this.global.state.defaultAddress = item
                    }
                })
            })

        },
        onDelete() {
            this.axios.get('/api/store/user/address/delete/' + this.address_id)
                .then(res => {
                    if (res.data.code == 200) {
                        Notify({
                            type: 'success',
                            background: '#07c160',
                            message: res.data.message,
                            onOpened: () => {
                                this.$router.go(-1)
                            }
                        });
                    }
                }).catch(err => {
                    console.log(err)
                })
        },
        changeDefaultAddress(val){
            console.log(11)
            console.log(val);
            
        },
        onChangeDetail(val) {

        },
        getDefaultAddress() {
            let params = {};
            params.token = sessionStorage.getItem("token");
            if (params.token == null) return;
            const defaultAddress = this.axios.get('/api/store/user/address/list/', {
                params
            })
            defaultAddress.then(res => {
                this.defaultType = true;
                res.data.data.data.forEach(item => {
                    if (item.default) {
                        this.global.state.defaultAddress = item
                    }
                });
            }).catch(err => err)

        },
    }
};
</script>

<style scoped>
</style>
